package com.xtourms.client.rm.ticket;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.xtourms.client.ContentFrame;
import com.xtourms.client.Context;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.client.ui.util.MoneyTextBox;
import com.xtourms.client.util.MysqlDateFormat;
import com.xtourms.client.util.Util;
import com.xtourms.shared.field.TicketField;
import com.xtourms.shared.field.WorkerField;

public class StatusDetailManager extends Composite {

	private static StatusDetailManagerUiBinder uiBinder = GWT
			.create(StatusDetailManagerUiBinder.class);

	interface StatusDetailManagerUiBinder extends
			UiBinder<Widget, StatusDetailManager> {
	}

	private JSONObject statusObj;
	private JSONObject ticketObj;
	private ContentFrame parent;
	private AsyncCallback<String> callback;
	
	@UiField Label ticketName;
	@UiField ListBox ticketType;
	@UiField MoneyTextBox basePrice;
	@UiField MoneyTextBox actualPrice;
	@UiField MoneyTextBox sellPrice;
	@UiField DateBox startDate;
	@UiField DateBox endDate;
	
	public StatusDetailManager(JSONObject ticketObj, ContentFrame parent, AsyncCallback<String> callback) {
		initWidget(uiBinder.createAndBindUi(this));
		this.statusObj = new JSONObject();
		this.ticketObj = ticketObj;
		this.parent = parent;
		this.callback = callback;
		init();
	}

	public StatusDetailManager(JSONObject ticketObj, JSONObject statusObj, ContentFrame parent, AsyncCallback<String> callback) {
		initWidget(uiBinder.createAndBindUi(this));
		this.statusObj = statusObj;
		this.ticketObj = ticketObj;
		this.parent = parent;
		this.callback = callback;
		init();
		initStatus();
	}

	private void init() {
		ticketName.setText(((JSONString)ticketObj.get(TicketField.TICKET_NAME)).stringValue());
		Util.fillListBox(ticketType, TicketContent.TICKET_TYPE_STRS);
		startDate.setFormat(new MysqlDateFormat());
		endDate.setFormat(new MysqlDateFormat());
	}
	
	private void initStatus() {
		String style = ((JSONString)statusObj.get(TicketField.STATUS_TYPE)).stringValue();
		int index = Util.findIndex(TicketContent.TICKET_TYPE_STRS, style);
		ticketType.setSelectedIndex(index);
		basePrice.setValue(((JSONNumber)statusObj.get(TicketField.STATUS_BASE_PRICE)).toString());
		actualPrice.setValue(((JSONNumber)statusObj.get(TicketField.STATUS_ACTUAL_PRICE)).toString());
		sellPrice.setValue(((JSONNumber)statusObj.get(TicketField.STATUS_SELL_PRICE)).toString());
		startDate.setValue(java.sql.Date.valueOf(((JSONString)statusObj.get(TicketField.STATUS_START_DATE)).stringValue()));
		endDate.setValue(java.sql.Date.valueOf(((JSONString)statusObj.get(TicketField.STATUS_END_DATE)).stringValue()));
	}
	
	@UiHandler("back")
	void back(ClickEvent evt){
		parent.resumeContent();
	}
	
	@UiHandler("confirm")
	void confirm(ClickEvent evt){
		if(!isValid()){
			return;
		}
		
		statusObj.put(TicketField.STATUS_TYPE, new JSONString(ticketType.getValue(ticketType.getSelectedIndex())));
		statusObj.put(TicketField.STATUS_START_DATE, new JSONString(startDate.getValue().toString()));
		statusObj.put(TicketField.STATUS_END_DATE, new JSONString(endDate.getValue().toString()));
		statusObj.put(TicketField.STATUS_BASE_PRICE, new JSONNumber(Integer.parseInt(basePrice.getValue())));
		statusObj.put(TicketField.STATUS_ACTUAL_PRICE, new JSONNumber(Integer.parseInt(actualPrice.getValue())));
		statusObj.put(TicketField.STATUS_SELL_PRICE, new JSONNumber(Integer.parseInt(sellPrice.getValue())));
		statusObj.put(TicketField.TICKET_ID, ticketObj.get(TicketField.TICKET_ID));
		statusObj.put(TicketField.LASTMODIFY_WORKER, Context.ins.worker.get(WorkerField.WORKER_ID));
		
		TicketManagerFrame.tmsa.cOrMStatus(statusObj.toString(), callback);
	}
	
	private boolean isValid(){
		if(null == startDate.getValue() || null == endDate.getValue()
				|| -1 == basePrice.getMoney() || -1 == sellPrice.getMoney()
				|| -1 == actualPrice.getMoney()){
			MessageBox.ins.showWarning("请填写必填项。");
			return false;
		}
		if(!(basePrice.isValid() && sellPrice.isValid() && actualPrice.isValid())){
			MessageBox.ins.showWarning("请检查输入金额是否符合规范。");
			return false;
		}
		//FIXME:必须高于费率才有利润
		if(sellPrice.getMoney()*0.97 <= basePrice.getMoney()){
			MessageBox.ins.showWarning("请检查销售价格是否过低。");
			return false;
		}
		if(sellPrice.getMoney() > actualPrice.getMaxLength()){
			MessageBox.ins.showWarning("请确认销售价格不应该高于挂牌价。");
			return false;
		}
		if(startDate.getValue().after(endDate.getValue())){
			MessageBox.ins.showWarning("终止日期不能早于起始日期。");
			return false;
		}
		return true;
	}
}
